<template>
  <div class="form-elements">
    <div class="row">
      <div class="flex xs12">
        <va-card :title="'修改楼层信息'">
          <form>
            <div class="row">
              <div class="flex md3 sm6 xs12">
                <va-input
                  v-model="number"
                  :label="'楼层号'"
                />
              </div>
             <div class="flex md3 sm6 xs12">
                <va-input
                  v-model="name"
                  :label="'楼层名称'"
                />
              </div>
            </div>
            <div class="row">
              <div class="flex md3 sm6 xs12">
                <va-input
                  v-model="content"
                  type="text"
                  :label="'楼层介绍'"
                />
              </div>
               <div class="flex md3 sm6 xs12">
                <va-input
                  v-model="createTime"
                  type="date"
                  :label="'创建时间'"
                />
              </div>
            </div>
            <div class="row">
              <div class="flex md3 sm6 xs12">
                <va-select
                  :label="'状态'"
                  v-model="status"
                  textBy="description"
                  :options="statusOptions"
                />
              </div>
               <div class="flex md3 sm6 xs12">
                        <va-card :title="'图片'">
          <va-file-upload
            type="single"
            v-model="imagefile"
            :title="'上传图片'"
          />
        </va-card>
              </div>
            </div>
            <div class="row">
              <va-button @click="subadd">提交</va-button>
              <va-button @click="back">返回</va-button>
            </div>
          </form>
        </va-card>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'checkout-add',
  data () {
    return {
      number:'',
      name:'',
      content:'',
      createTime:'',
      status:{},
      imagefile:[],
      imageurl:'',
      statusOptions:  [
        {
          id: 0,
          description: '未开',
        },
        {
          id: 1,
          description: '正常',
        }
      ],
    }
  },
  created(){
    if(!this.$route.params.id)
       this.$router.push({ name: 'dashboard' })

    var cidata=JSON.parse(JSON.stringify(this.$route.params))
    console.log(cidata)
  if(cidata.createTime!=null)
    this.createTime=cidata.createTime.split(" ")[0]

    this.number=cidata.number
    this.name=cidata.name
    this.content=cidata.content
    this.status=cidata.status=='正常'?{id: 1,description: '正常'}:cidata.status=='未开'?{id: 0,description: '未开'}:{}
    this.imageurl=cidata.imageurl
  },


  methods: {
    subadd () {
     if(this.number==''){
        this.showmsg('楼层号为空')
        return
      }
      var _this=this
      if(this.imagefile.length>0){
var fd = new FormData()
      fd.append('file',this.imagefile[this.imagefile.length-1])
      this.$http.post('http://8.135.35.123:9000/hotelsystem/floor/uploadImg',fd)
      .then(function(response) {
        if(response.data.code==80200) {
          _this.imageurl=response.data.data
          _this.showmsg('图片上传成功')
        }
        else
        _this.showmsg('图片上传失败')
post()
        },
        function(err){
          _this.showmsg('图片上传失败')
          console.log(err)
          post()
        }
      )
      }
      else
        post()

function post() {
  console.log(_this.imageurl)


      var fd = new URLSearchParams()
      fd.append('id',_this.$route.params.id)
      if(_this.createTime!=""){
        let dt=new Date(_this.createTime)
        fd.append('createTime',dt.toString())
      }
      fd.append('content',_this.content)
      fd.append('image',_this.imageurl)
      fd.append('name',_this.name)
      fd.append('number',_this.number)
      if(_this.status)
fd.append('status',_this.status.id)
      _this.$http.post('http://8.135.35.123:9000/hotelsystem/floor/mod',fd)
      .then(function(response) {
        if(response.data.code==80200) {
        _this.showmsg('修改成功')
        _this.$router.push({ name: 'floor-list' })
        }
        else
        _this.showmsg('修改失败')
        },
        function(err){
          console.log(err)
        }
      )
}
    },
        back(){
        this.$router.push({ name: 'floor-list' })
    },
    showmsg(msg) {
    this.showToast(
        msg,{
          position: 'top-center',
          duration: 2500,
          fullWidth: false,
        },
      )

    }
  },
}
</script>

<style>
  .row.row-inside {
    max-width: none;
  }
</style>
